package com.zbkj.admin.task.integral;


import cn.hutool.core.util.NumberUtil;
import com.zbkj.common.constants.IntegralDetailConstants;
import com.zbkj.common.model.Integral.IntegralParameter;
import com.zbkj.common.model.user.User;
import com.zbkj.common.model.user.UserIntegral;
import com.zbkj.common.model.user.UserIntegralDetail;
import com.zbkj.common.utils.DateUtil;
import com.zbkj.service.service.*;
import com.zbkj.service.util.IntegralUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * 积分冻结期解冻task
 * +----------------------------------------------------------------------
 * | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 * +----------------------------------------------------------------------
 * | Author: CRMEB Team <admin@crmeb.com>
 * +----------------------------------------------------------------------
 */
@Component
@Configuration //读取配置
@EnableScheduling // 2.开启定时任务
public class IntegralDaliyTask {

    //日志
    private static final Logger logger = LoggerFactory.getLogger(IntegralDaliyTask.class);

    @Autowired
    private UserIntegralRecordService userIntegralRecordService;

    @Autowired
    private UserIntegralService userIntegralService;

    @Autowired
    private UserService userService;

    @Autowired
    private IntegralParameterService integralParameterService;

    @Autowired
    private IntegralUtil integralUtil;

    @Autowired
    private StoreOrderService storeOrderService;

    @Scheduled(cron = "0/59 * * * * ?  ") //1分钟同步一次数据
    public void init() {
        logger.info("---IntegralFrozenTask task------produce Data with fixed rate task: Execution Time - {}", DateUtil.nowDateTime());
        try {
            List<User> allUser = userService.getAllUser();
            allUser.forEach(user -> {
                IntegralParameter integralParameter = integralParameterService.getIntegralParameter(1);
                BigDecimal rate = integralParameter.getReleaseParameter().divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP);
                UserIntegral userIntegral = userIntegralService.getById(user.getUid());
                BigDecimal greenIntegral = userIntegral.getGreenIntegral();
                BigDecimal changeValue = greenIntegral.multiply(rate);
                if (!NumberUtil.isGreater(userIntegral.getGreenIntegralFreeze(),BigDecimal.ZERO)){
                    userIntegralService.operationGreenIntegralFreeze(user.getUid(),changeValue,userIntegral.getGreenIntegralFreeze(),IntegralDetailConstants.CHANGE_TYPE_DEC);
                    String greenReason = "解冻了"+ changeValue + IntegralDetailConstants.INTEGRAL_TYPE_GREEN;
                    integralUtil.saveIntegralDetail(user.getUid(),IntegralDetailConstants.INTEGRAL_TYPE_GREEN,IntegralDetailConstants.CHANGE_TYPE_ADD,changeValue,greenReason,userIntegral.getGreenIntegral());
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("IntegralFrozenTask.task" + " | msg : " + e.getMessage());
        }
    }
}
